var fun1 = require('./ruleWin');
const Websocket = require('ws')
// 启动一个websocket服务
const ws = new Websocket.Server({
    port: 8887
})



// 定义一个空数组，用来保存连接上的客户端



// 封装一个群发方法
const sendAll = (data) => {
    for(let e of clients){
        e.client.send(JSON.stringify(data))
    }
}
// 发送给其他玩家
const sendOther=(id,data)=>{
    for(let e of clients){
        if(e.id!=id){
            e.client.send(JSON.stringify(data))
        }
        
    }
}
var timer=0;
const clients = []; //玩家数组
var chessboard=new Array(225).fill(null);
var playState=1;// 1为未开始,2为已开始 当有两位玩家时 给两位玩家发送信息,可以开始,且把状态改为2,3未赢了,4为用户退出
var nextPlay="black"; //第一步为黑棋先走,然后变为白棋
var nextPlayId=""; //第一次为先进来的用户,走棋后轮换
function checkWinner(index) {
    return fun1.ruleWin(chessboard, index)
    
}
function init(params) {
    
}

// 监听客户端的连接
ws.on('connection',(client,req) => {
    // 保存当前连接上的客户端
    const ip = req.socket.remoteAddress;
    const id=Math.random();
    let status
    if(clients.length==0){
         status="black";
         nextPlayId=id;
    }else if(clients.length==1){
         status=clients[0].status=="white"?"black":"white";
         nextPlayId=clients[0].status=="white"?id:nextPlayId
    }else{
        status="red"
    }
    client.id=id;
    let isReady=false;
    
    clients.push({client,ip,id,status,isReady})
    // 用户初始化
    const data={
        playState:1,  //游戏是否开始
        ip,  //用户ip
        id, //用户id
        status, //用户棋色
        nextPlay, //下一步走棋棋色
        nextPlayId, //下一步走棋的用户id
    }
    client.send(JSON.stringify(data))
    if(clients.length==2){
        // 游戏开始
        const data2={
            playState:2,
            chessboard,//游戏开始时需要初始化棋盘
            nextPlayId,
            nextPlay
        }
        setTimeout(function(){
            sendAll(data2)

        },100)
       
    }

    // 监听客户端发来的消息
    client.on('message',(res) => {
        const {id,index,status,type}=JSON.parse(res)
        if(type && type=="again"){
            sendOther(id,{
                status,
                playState:5    //再来一次
            })
        }else if(type =="againOk"){
            chessboard=new Array(225).fill(null)  //初始化棋盘
            nextPlay="black",
            clients.forEach((item)=>{
                nextPlayId=item.status=="black"?item.id:nextPlayId
            })
            sendAll({
                nextPlayId,
                nextPlay,
                playState:2
            })
        }else if(type =="againNo"){
            sendOther(id,{
                playState:6 
            })
        }else if(type=="ready"){
            
        }else{
            if(id==nextPlayId){
                chessboard[index]=status
                playState=3;
                clients.forEach((item)=>{
                    if(item.id!=id){
                        nextPlayId=item.id;
                        nextPlay=item.status
                    }
                })
                let winner=checkWinner(index)
                let obj={
                    index,
                    nextPlay,
                    nextPlayId,
                    status,
                    playState,
                    winner
                }
                sendAll(obj)
            }
        }
        
        // 
        
    })

    // 监听客户端断开
    client.on('close',(req,bbb) => {
        // arr.splice(1,1)
        clients.forEach(function(item,index){
            if(item.id==client.id){
                
                chessboard=new Array(225).fill(null)
                if(item.id==nextPlayId){
                    if(item.status=="black"){
                        nextPlayId="";
                    }else{
                        nextPlayId=index==0?clients[1].id:client[0].id;
                        
                    }
                   
                    nextPlay="black"
                }
                sendAll({
                    playState:4,
                    status:item.status,
                    nextPlayId,
                    nextPlay
                })
            }
            clients.splice(index,1) 
        })
    })
})